﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.ad
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString();      //操作类型
        public int id;
        public int position_id;
        public string imgurl = "";
        public Model.ad_position ad_position;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型

                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
                Model.ad model = new BLL.ad().GetModel(this.id);
                if (model == null)
                {
                    JscriptMsg(350, 230, "错误提示", "信息不存在或已被删除！", "back", "Error");
                    return;
                }
                position_id = model.position_id;
            }
            else
            {
                if (!int.TryParse(Request.Params["position_id"] as string, out this.position_id))
                {
                    JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                    return;
                }
            }

            ad_position = new BLL.ad_position().GetModel(position_id);

            if (!IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString()) //修改
                {
                    chkLoginLevel("editAd");
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addAd");
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.ad model = new He.BLL.ad().GetModel(_id);
            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            txtLinkUrl.Text = model.link_url;
            chkRed.Checked = model.is_red == 1 ? true : false;
            txtSortId.Text = model.sort_id.ToString();

            imgurl = model.img_url;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.ad bll = new BLL.ad();
            Model.ad model = new Model.ad();
            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.is_red = chkRed.Checked ? 1 : 0;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.position_id = position_id;
            if (bll.Add(model) > 0)
            {
                //重写config.xml文件
                Model.ad_position mposition = new BLL.ad_position().GetModel(model.position_id);
                if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
                {
                    WriteCu3erXml(mposition.id);
                }
                //更新缓存
                RemoveAdCache(mposition.call_mark);
                SaveLogs("[广告管理]增加广告：" + model.title);
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.ad bll = new BLL.ad();
            Model.ad model = bll.GetModel(_id);
            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.link_url = txtLinkUrl.Text.Trim();
            model.is_red = chkRed.Checked ? 1 : 0;
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.position_id = position_id;
            if (bll.Update(model))
            {
                Model.ad_position mposition = new BLL.ad_position().GetModel(model.position_id);
                //重写config.xml文件
                if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
                {
                    WriteCu3erXml(mposition.id);
                }
                //更新缓存
                RemoveAdCache(mposition.call_mark);
                SaveLogs("[广告管理]修改广告：" + model.title);
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                string url = CookieHelper.Get("LastListUrl");
                if (string.IsNullOrEmpty(url))
                {
                    url = Request.RawUrl;
                }

                if (!DoEdit(this.id))
                {

                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", Request.RawUrl, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", Request.RawUrl, "Success");
            }
        }

    }
}
